package com.myk.game.gobangchess.rooms;

import com.myk.game.gobangchess.CustomBottom;
import com.myk.game.gobangchess.Player;
import com.myk.game.gobangchess.Sign;
import com.myk.game.gobangchess.constants.ColorCost;
import com.myk.game.gobangchess.constants.WinMode;
import com.myk.game.gobangchess.rooms.absrooms.AbsChessRoom;
import com.myk.game.gobangchess.rooms.absrooms.movechess.MoreColorCost;
import com.myk.game.gobangchess.signs.*;
import com.myk.game.gobangchess.utils.GridsUtils;

import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * 翻转象棋
 */
public class FlipChessRoom extends AbsChessRoom {

    private CacheSign chessBoardCacheSign; //背景缓存标记
    private final List<int[]> moveArrayList = new ArrayList<>(); // 棋子移动标记
    private int[] turnPoint = null; //棋子翻转标记
    private List<Chess> chessList; //场上棋子集合
    private Chess choose; //当前选中的棋子
    private Chess hasMovedChess; //当前动过的棋子, 不可以一回合移动翻转同一个棋子
    private int hasMovedMode = 0; //本回合行动状态记录  0：未行动， 1：已移动，  2：已翻转，

    /**
     * 翻转棋子按钮
     */
    private final CustomBottom turnOverBottom = new CustomBottom(this, "翻转", new CustomBottom.PlayerEvent() {
        @Override
        public void event(Player player) {
            //基本的判空操作
            if (choose != null){
                turnPoint = new int[]{choose.row, choose.col}; //记录反转点
                hasMovedMode = 2; //状态改为已翻转
                choose.flip(); //棋子执行翻转
                checkGameOver(); //检查游戏是否结束
            }
            //此时应该是异常情况，直接清除按钮即可
            else {
                player.clearCustomBottom();
                player.dispatchCustomBottom();
            }
        }
    });

    public FlipChessRoom(String roomName) {
        //（作者不希望把自己的名字打出来，简介里不要提）
        super(roomName, "逆界群友设计的小型象棋，具有翻转切换棋子的特殊能力。", "翻转象棋", GridsUtils.createEmptyGrids(7, 7), true, true);
        initChessBoardCacheSign();
        initChessList();
    }

    //棋盘背景的设计
    private void initChessBoardCacheSign() {
        List<Sign> chessboardStyle = new ArrayList<>();
        int row = curGrids.length;
        int col = curGrids[0].length;

        //画透明桌布遮盖默认线条
        chessboardStyle.add(new GroundSign(getIndex(0, 0), getIndex(row-1, col-1), ColorCost.TABLECLOTH_COLOR, ColorCost.TABLECLOTH_COLOR, 2.2f, 0, 0));

        //记录所有格子索引
        List<Integer> allGridsIndexes = new ArrayList<>();
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                allGridsIndexes.add(getIndex(i, j));
            }
        }

        //画底部纹饰
        chessboardStyle.add(FigureSign.makeBatchSingleFigure(allGridsIndexes, "#44888888").setSideExpArc(1,1,1,1,1,1,1,1));

        //画白色半透明格子
        for (Integer gridsIndex : allGridsIndexes) {
            chessboardStyle.add(new AroundSign(gridsIndex, "#66FFFFFF", ColorCost.TABLECLOTH_COLOR, 1f));
        }

        //画底部纹饰
        chessboardStyle.add(FigureSign.makeBatchSingleFigure(allGridsIndexes, ColorCost.TABLECLOTH_COLOR).setMidSideLine(1,1,1,1,1,1,1,1));

        //将背景封装为缓存标记
        chessBoardCacheSign = new CacheSign(this,"chessBoardCacheSign",10,true,chessboardStyle); //*** 修改背景样式记得改这里的版本号！！！
    }

    @Override
    public CacheSign getCacheSignByName(String cacheName) {
        return chessBoardCacheSign;
    }

    @Override
    public String getRuleText() {
        return "《翻转象棋》\n" +
                "棋子：\n" +
                "红方：马/向；马/车；抓/伢；帅/仕\n" +
                "蓝方：馬/项；馬/車；爪/牙；将/士\n" +
                "中立：冰/足（三个）\n" +
                "\n" +
                "摆放方式：\n" +
                "在7×7的棋盘上双方把棋子摆放的旋转对称，马/馬和帅/将面向上，冰足先手方中间的冰/足与我方同色，两边相反，如下图\n" +
                "马/向━━━帅/仕━━━马/车\n" +
                "┣━╋━╋━╋━╋━╋━┫\n" +
                "┣━抓/伢━╋━╋━抓/伢━╋━┫\n" +
                "足/冰━━━冰/足━━━足/冰\n" +
                "┣━╋━爪/牙━╋━╋━爪/牙━┫\n" +
                "┣━╋━╋━╋━╋━╋━┫\n" +
                "馬/車━━━将/士━━━馬/项\n" +
                "\n" +
                "走法差异（区别于中象）：\n" +
                "1.抓/爪可往左右后方斜后方走1格，伢/牙可往左右前方斜前方走1格\n"+
                "2.向/项为国际象棋的象，相当于斜着走的车\n" +
                "3.冰始终能横走但同样不能后退，且能同向走1～2格\n" +
                "4.将/帅和士可以走到任何地方，且没有对脸规则\n" +
                "除此之外与象棋完全相同\n" +
                "\n" +
                "游戏玩法：\n" +
                "1.由冰/足少的一方成为先手玩家\n" +
                "2.每回合可以从己方翻转一枚棋子，移动一枚棋子，不可以移动翻转同一个棋子，上回合刚被对手翻到的冰/足不能马上翻面（移动的没有这项限制），可以决定翻转和移动的先后顺序\n" +
                "3.可以翻转对方的冰/足\n" +
                "\n" +
                "胜利条件：\n" +
                "1.吃掉对手的将/帅棋子（吃掉士那一面也算）\n" +
                "2.对手回合开始时只有一枚棋子（包含冰/足）";
    }

    @Override
    public void resetGrids() {
        super.resetGrids();
        moveArrayList.clear();
        turnPoint = null;
        choose = null;
        hasMovedChess = null;
        hasMovedMode = 0;
        initChessList();
    }

    private void initChessList() {
        chessList = new ArrayList<>();
        chessList.add(new Bishop(this,1, 0, 0));
        chessList.add(new King(this,1, 0, 3));
        chessList.add(new Chariot(this,1, 0, 6));
        chessList.add(new Henchman(this,1, 2, 1,true));
        chessList.add(new Henchman(this,1, 2, 4,false));
        //21牙，24爪
        chessList.add(new Soldier(this,2, 3, 0));
        chessList.add(new Soldier(this,1, 3, 3));
        chessList.add(new Soldier(this,2, 3, 6));
        chessList.add(new Henchman(this,2, 4, 2,false));
        chessList.add(new Henchman(this,2, 4, 5,true));
        chessList.add(new Chariot(this,2, 6, 0));
        chessList.add(new King(this,2, 6, 3));
        chessList.add(new Bishop(this,2, 6, 6));
    }

    @Override
    public String getGameInfo() {
        //先全部初始化
        curGrids = GridsUtils.copyGrids(originGrids);
        signList.clear();
        //画棋盘背景
        signList.add(chessBoardCacheSign);
        //根据chessList集合和choose对象来生成要显示到客户端的布局和标记
        if (chessList != null && chessList.size() > 0) {
            //绘制棋子
            for (Chess chess : chessList) {
                boolean isBlackChess = chess.side == 1;
                boolean isSoldier = chess instanceof Soldier;
                int index = getIndex(chess.row, chess.col);
                curGrids[chess.row][chess.col] = isBlackChess ? 3 : 4; //显示棋子的底座
                signList.add(new TextSign(index, chess.frontName, isBlackChess ? ColorCost.BLACK : ColorCost.WHITE)); //显示棋子的文字
                if (isSoldier) {
                    signList.add(new BadgeSign(index, chess.backName, 2, isBlackChess ? ColorCost.WHITE : ColorCost.BLACK, isBlackChess ? ColorCost.BLUE : ColorCost.RED));
                }
                else {
                    signList.add(new BadgeSign(index, chess.backName, 2, isBlackChess ? ColorCost.BLACK : ColorCost.WHITE, isBlackChess ? ColorCost.RED : ColorCost.BLUE));
                }
            }
            //绘制对弈时的选中棋子移动状态
            if (choose != null) {
                //绘制棋子可移动路径
                List<Integer> moveRange = choose.getMoveRange(); //获取该棋子可移动到的格子的坐标集合
                if (moveRange.size() > 0) {
                    signList.add(new ColorSign(moveRange, ColorCost.NEW_PUT_POINT_LIGHT)); //可移动到的格子用更淡的绿色表示
                }
                //画一下吃子范围
                List<Integer> eatRange = choose.getEatRange();
                if (eatRange.size() > 0){
                    signList.add(new ColorSign(eatRange, MoreColorCost.EAT));
                }
                //绘制棋子选中状态
                signList.add(new GroundSign(getIndex(choose.row, choose.col), ColorCost.TRANSPARENT, ColorCost.NEW_PUT_POINT, 2F, 0, 1));
            }
            //绘制移动提示
            for (int[] moveArray : moveArrayList) {
                signList.add(new LineSign(moveArray[0], moveArray[1], ColorCost.NEW_PUT_POINT_LIGHT, 1));
            }
            //绘制翻转提示
            if (turnPoint != null){
                signList.add(new TextSign(getIndex(turnPoint[0], turnPoint[1]),"⇋",ColorCost.NEW_PUT_POINT_LIGHT));
            }
        }
        return super.getGameInfo();
    }

    @Override
    public String getCoordMode() {
        return "31310000";
    }

    @Override
    public void doRule(int row, int col, int value) {
        //未选中棋子时
        if (choose == null){
            Chess chess = getChessByPos(row, col);
            //点击的棋子是己方棋子或者中立棋子时,且不是刚行动的棋子时，可以选中
            if (chess != null && (chess.side == value || chess instanceof Soldier) && chess != hasMovedChess) {
                this.choose = chess; //选中该棋子
                dispatchTurnOverBottom(); //分发翻转按钮
                noticeAllRefreshGameInfo(); //通知界面刷新
            }
        }
        //已有选中棋子时
        else {
            Chess newChess = getChessByPos(row, col);
            //再点击空格
            if (newChess == null) {
                //点击可移动到的空位时
                if (choose.getMoveRange().contains(getIndex(row, col))) {
                    //更新移动标记
                    moveArrayList.clear();
                    moveArrayList.add(new int[]{choose.getIndex(), getIndex(row, col)});
                    //记录本回合移动状态
                    hasMovedMode = 1;
                    //更新棋子坐标
                    choose.row = row;
                    choose.col = col;
                    //检查回合是否结束
                    checkGameOver();
                }
                //点击不可以移动的位置时取消选中
                else {
                    this.choose = null;
                    dispatchTurnOverBottom();
                    noticeAllRefreshGameInfo();
                }
            }
            //再点击己方棋子 或者 公共棋子冰/足时
            else if (newChess.side == value) {
                this.choose = newChess; //切换选中棋子
                dispatchTurnOverBottom();
                noticeAllRefreshGameInfo();
            }
            //再点击对方棋子时
            else {
                //点击可吃的棋子时
                if (choose.getEatRange().contains(newChess.getIndex())) {
                    //更新移动标记
                    moveArrayList.clear();
                    moveArrayList.add(new int[]{choose.getIndex(), newChess.getIndex()});
                    //记录本回合移动状态
                    hasMovedMode = 1;
                    //被吃棋子移到拘台
                    chessList.remove(newChess);
                    //更新棋子坐标
                    choose.row = newChess.row;
                    choose.col = newChess.col;
                    //检查回合是否结束（切换行动方）
                    checkGameOver();
                }
                //点击不可吃的对方棋子时，取消选中
                else {
                    this.choose = null;
                    dispatchTurnOverBottom();
                    noticeAllRefreshGameInfo();
                }
            }
        }
    }

    @Override
    protected void prepareInGameButtons() {
        dispatchTurnOverBottom(); //重新入座时分发按钮
    }

    //检测是否分发翻转按钮
    private void dispatchTurnOverBottom() {
        if (overFlag == 1 && hasMovedMode != 2 && choose != null && choose.isCanFlip() && choose != hasMovedChess){
            if (cur == 1){
                if (player_white!=null){
                    player_white.removeCustomBottom(turnOverBottom);
                    player_white.dispatchCustomBottom();
                }
                if (player_black!=null){
                    player_black.registerCustomBottom(turnOverBottom);
                }
            }
            else {
                if (player_black!=null){
                    player_black.removeCustomBottom(turnOverBottom);
                    player_black.dispatchCustomBottom();
                }
                if (player_white!=null){
                    player_white.registerCustomBottom(turnOverBottom);
                }
            }
        }
        else {
            if (player_black!=null){
                player_black.removeCustomBottom(turnOverBottom);
                player_black.dispatchCustomBottom();
            }
            if (player_white!=null){
                player_white.removeCustomBottom(turnOverBottom);
                player_white.dispatchCustomBottom();
            }
        }
    }

    //检测游戏是否结束
    private void checkGameOver() {
        //1.吃掉对手的将/帅棋子（吃掉士那一面也算）
        if (!hasChessType(chessList, King.class,2)) {
            stepAdd(); //增加回合数记录
            doOver(WinMode.BLACK_WIN, "黑方获胜！");
            dispatchTurnOverBottom();
            return;
        }
        else if (!hasChessType(chessList, King.class,1)) {
            stepAdd(); //增加回合数记录
            doOver(WinMode.WHITE_WIN, "白方获胜！");
            dispatchTurnOverBottom();
            return;
        }

        //本回合第一步，还有下一步
        if (hasMovedChess == null){
            hasMovedChess = choose; //记录行动过的棋子
            choose = null;
            dispatchTurnOverBottom();
            noticeAllRefreshGameInfo();
        }
        //本回合第二步结束，切换回合
        else {
            stepAdd(); //增加回合数记录
            hasMovedChess = null; //取消本回合行动记录
            hasMovedMode = 0; //取消本回合行动记录
            choose = null;
            dispatchTurnOverBottom();

            //2.对手回合开始时只有一枚棋子（包含冰/足）
            int whiteNum = 0; //统计白方剩余棋子数量
            for (Chess chess : chessList) {
                if (chess.side == 2) whiteNum++;
            }
            if (whiteNum <= 1) {
                doOver(WinMode.BLACK_WIN, "黑方获胜！");
                dispatchTurnOverBottom();
                return;
            }
            int blackNum = 0;
            for (Chess chess : chessList) {
                if (chess.side == 1) blackNum++;
            }
            if (blackNum <= 1) {
                doOver(WinMode.WHITE_WIN, "白方获胜！");
            }

            //切换行动方
            changeNoticeMove();
        }
    }

    //检测棋子集合中是否包含某个类型的棋子
    public boolean hasChessType(List<Chess> list, Class<? extends Chess> chessType, int side) {
        for (Chess chess : list) {
            if (chess.getClass() == chessType && chess.side == side) {
                return true;
            }
        }
        return false;
    }

    //切换行动方
    public void changeNoticeMove() {
        if (cur == 1) {
            noticeWhiteMove();
        }
        else {
            noticeBlackMove();
        }
    }

    //根据坐标搜索棋子 （有多个棋子时取城门之上的棋子, 可为空）
    public Chess getChessByPos(int row, int col) {
        for (Chess chess : chessList) {
            if (chess.row == row && chess.col == col) {
                return chess;
            }
        }
        return null;
    }

    /**
     * 棋子基类
     */
    private static abstract class Chess {

        public FlipChessRoom room;
        public String frontName;
        public String backName;
        public int side;
        public int row;
        public int col;

        public Chess(FlipChessRoom room,int side, int row, int col) {
            this.room = room;
            this.side = side;
            this.row = row;
            this.col = col;
        }

        //获取棋子索引
        public Integer getIndex() {
            return room.getIndex(row, col);
        }

        //翻转棋子
        public void flip() {
            String tempName = frontName;
            this.frontName = backName;
            this.backName = tempName;
        }

        //是否可翻转
        public boolean isCanFlip() {
            return true;
        }

        /**
         * 自定义 获取可移动空格范围
         */
        public abstract List<Integer> getMoveRange();

        /**
         * 自定义 获取可吃棋子范围
         */
        public abstract List<Integer> getEatRange();

        //根据规则搜索棋子默认的移动范围
        protected List<Integer> defMoveRange(int[][] dxdys, int distance) {
            if (room.hasMovedMode == 1){
                return new ArrayList<>(); //本回合已经移动过棋子了就不能再移动只能翻转
            }
            return defMoveRange(dxdys, distance, 0, 0);
        }

        //根据规则搜索棋子默认的移动范围
        protected List<Integer> defMoveRange(int[][] dxdys, int distance, int canJump, int mustJump) {
            List<Integer> rangeIndex = new ArrayList<>();
            for (int[] dxdy : dxdys) {
                int skip = 0;
                for (int x = row + dxdy[0], y = col + dxdy[1], i = 0; x >= 0 && x < room.curGrids.length && y >= 0 && y < room.curGrids[0].length && i < distance; x += dxdy[0], y += dxdy[1], i++) {
                    Chess chess = room.getChessByPos(x, y);
                    if (chess != null) { //否则遇到棋子阻碍就得停下
                        skip++;
                        if (skip > canJump) {
                            break;
                        }
                        continue;
                    }
                    if (skip >= mustJump) {
                        rangeIndex.add(room.getIndex(x, y));
                    }
                }
            }
            return rangeIndex;
        }

        //根据规则搜索棋子默认的吃子范围
        protected List<Integer> defEatRange(int[][] dxdys, int distance) {
            if (room.hasMovedMode == 1){
                return new ArrayList<>(); //本回合已经移动过棋子了就不能再吃子只能翻转
            }
            return defEatRange(dxdys, distance, 0, 0);
        }

        //根据规则搜索棋子默认的吃子范围
        protected List<Integer> defEatRange(int[][] dxdys, int distance, int canJump, int mustJump) {
            List<Integer> rangeIndex = new ArrayList<>();
            for (int[] dxdy : dxdys) {
                int skip = 0;
                for (int x = row + dxdy[0], y = col + dxdy[1], i = 0; x >= 0 && x < room.curGrids.length && y >= 0 && y < room.curGrids[0].length && i < distance; x += dxdy[0], y += dxdy[1], i++) {
                    Chess chess = room.getChessByPos(x, y);
                    if (chess != null) {
                        if (chess.side != this.side && skip >= mustJump) {
                            rangeIndex.add(room.getIndex(x, y));
                        }
                        skip++;
                        if (skip > canJump) {
                            break;
                        }
                    }
                }
            }
            return rangeIndex;
        }

    }

    /**
     * 将帅和士仕
     */
    private static class King extends Chess {

        public King(FlipChessRoom room,int side, int row, int col) {
            super(room,side, row, col);
            if (side == 1){
                this.frontName = "帅";
                this.backName = "仕";
            }
            else {
                this.frontName = "将";
                this.backName = "士";
            }
        }

        @Override
        public List<Integer> getMoveRange() {
            if (frontName.equals("将") || frontName.equals("帅")){
                return defMoveRange(new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}, 1);
            }
            else {
                return defMoveRange(new int[][]{{1, 1}, {-1, -1}, {1, -1}, {-1, 1}}, 1);
            }
        }

        @Override
        public List<Integer> getEatRange() {
            if (frontName.equals("将") || frontName.equals("帅")){
                return defEatRange(new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}, 1);
            }
            else {
                return defEatRange(new int[][]{{1, 1}, {-1, -1}, {1, -1}, {-1, 1}}, 1);
            }
        }
    }

    /**
     * 车马
     */
    private static class Chariot extends Chess {

        public Chariot(FlipChessRoom room,int side, int row, int col) {
            super(room,side, row, col);
            if (side == 1){
                this.frontName = "馬";
                this.backName = "車";
            }
            else {
                this.frontName = "马";
                this.backName = "车";
            }
        }

        @Override
        public List<Integer> getMoveRange() {
            if (frontName.equals("馬") || frontName.equals("马")){
                List<Integer> range = new ArrayList<>();
                if (room.getChessByPos(row + 1, col) == null) {
                    range.addAll(defMoveRange(new int[][]{{2, 1}, {2, -1}}, 1));
                }
                if (room.getChessByPos(row - 1, col) == null) {
                    range.addAll(defMoveRange(new int[][]{{-2, 1}, {-2, -1}}, 1));
                }
                if (room.getChessByPos(row, col + 1) == null) {
                    range.addAll(defMoveRange(new int[][]{{1, 2}, {-1, 2}}, 1));
                }
                if (room.getChessByPos(row, col - 1) == null) {
                    range.addAll(defMoveRange(new int[][]{{1, -2}, {-1, -2}}, 1));
                }
                return range;
            }
            else {
                return defMoveRange(new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}, Integer.MAX_VALUE);
            }
        }

        @Override
        public List<Integer> getEatRange() {
            if (frontName.equals("馬") || frontName.equals("马")){
                List<Integer> range = new ArrayList<>();
                if (room.getChessByPos(row + 1, col) == null) {
                    range.addAll(defEatRange(new int[][]{{2, 1}, {2, -1}}, 1));
                }
                if (room.getChessByPos(row - 1, col) == null) {
                    range.addAll(defEatRange(new int[][]{{-2, 1}, {-2, -1}}, 1));
                }
                if (room.getChessByPos(row, col + 1) == null) {
                    range.addAll(defEatRange(new int[][]{{1, 2}, {-1, 2}}, 1));
                }
                if (room.getChessByPos(row, col - 1) == null) {
                    range.addAll(defEatRange(new int[][]{{1, -2}, {-1, -2}}, 1));
                }
                return range;
            }
            else {
                return defEatRange(new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}, Integer.MAX_VALUE);
            }
        }

    }

    /**
     * 向马
     */
    private static class Bishop extends Chess {

        public Bishop(FlipChessRoom room,int side, int row, int col) {
            super(room,side, row, col);
            if (side == 1){
                this.frontName = "馬";
                this.backName = "项";
            }
            else {
                this.frontName = "马";
                this.backName = "向";
            }
        }

        @Override
        public List<Integer> getMoveRange() {
            if (frontName.equals("馬") || frontName.equals("马")){
                List<Integer> range = new ArrayList<>();
                if (room.getChessByPos(row + 1, col) == null) {
                    range.addAll(defMoveRange(new int[][]{{2, 1}, {2, -1}}, 1));
                }
                if (room.getChessByPos(row - 1, col) == null) {
                    range.addAll(defMoveRange(new int[][]{{-2, 1}, {-2, -1}}, 1));
                }
                if (room.getChessByPos(row, col + 1) == null) {
                    range.addAll(defMoveRange(new int[][]{{1, 2}, {-1, 2}}, 1));
                }
                if (room.getChessByPos(row, col - 1) == null) {
                    range.addAll(defMoveRange(new int[][]{{1, -2}, {-1, -2}}, 1));
                }
                return range;
            }
            else {
                return defMoveRange(new int[][]{{1, 1}, {-1, 1}, {1, -1}, {-1, -1}}, Integer.MAX_VALUE);
            }
        }

        @Override
        public List<Integer> getEatRange() {
            if (frontName.equals("馬") || frontName.equals("马")){
                List<Integer> range = new ArrayList<>();
                if (room.getChessByPos(row + 1, col) == null) {
                    range.addAll(defEatRange(new int[][]{{2, 1}, {2, -1}}, 1));
                }
                if (room.getChessByPos(row - 1, col) == null) {
                    range.addAll(defEatRange(new int[][]{{-2, 1}, {-2, -1}}, 1));
                }
                if (room.getChessByPos(row, col + 1) == null) {
                    range.addAll(defEatRange(new int[][]{{1, 2}, {-1, 2}}, 1));
                }
                if (room.getChessByPos(row, col - 1) == null) {
                    range.addAll(defEatRange(new int[][]{{1, -2}, {-1, -2}}, 1));
                }
                return range;
            }
            else {
                return defEatRange(new int[][]{{1, 1}, {-1, 1}, {1, -1}, {-1, -1}}, Integer.MAX_VALUE);
            }
        }


    }

    /**
     * 爪牙
     */
    private static class Henchman extends Chess {

        public Henchman(FlipChessRoom room,int side, int row, int col,boolean isFlip) {
            super(room,side, row, col);
            if (side == 1){
                this.frontName = "抓";
                this.backName = "伢";
            }
            else {
                this.frontName = "爪";
                this.backName = "牙";
            }
            if (isFlip){
                flip();
            }
        }

        @Override
        public List<Integer> getMoveRange() {
            if (frontName.equals("抓")){
                return defMoveRange(new int[][]{{1, 0}, {1, -1}, {1, 1},  {0, -1}, {0, 1}}, 1);
            }
            if (frontName.equals("爪")){
                return defMoveRange(new int[][]{{-1, 0},{-1, -1}, {-1, 1},{0, -1}, {0, 1}}, 1);
            }
            if (frontName.equals("伢")){
                return defMoveRange(new int[][]{{-1, 0}, {-1, -1}, {-1, 1},{0, -1}, {0, 1}}, 1);
            }
            if (frontName.equals("牙")){
                return defMoveRange(new int[][]{{1, 0},{1, -1}, {1, 1}, {0, -1}, {0, 1}}, 1);
            }
            return null;
        }

        @Override
        public List<Integer> getEatRange() {
            if (frontName.equals("抓")){
                return defEatRange(new int[][]{ {1, 0}, {1, -1}, {1, 1},{0, -1}, {0, 1}}, 1);
            }
            if (frontName.equals("爪")){
                return defEatRange(new int[][]{{-1, 0},{-1, -1}, {-1, 1}, {0, -1}, {0, 1}}, 1);
            }
            if (frontName.equals("伢")){
                return defEatRange(new int[][]{{-1, 0}, {-1, -1}, {-1, 1},{0, -1}, {0, 1}}, 1);
            }
            if (frontName.equals("牙")){
                return defEatRange(new int[][]{{1, 0},{1, -1}, {1, 1}, {0, -1}, {0, 1}}, 1);
            }
            return null;
        }


    }

    /**
     * 冰/足
     */
    private static class Soldier extends Chess {

        //记录翻转的回合数
        private int flipStep = -99;

        public Soldier(FlipChessRoom room,int side, int row, int col) {
            super(room,side, row, col);
            if (side == 1){
                this.frontName = "冰";
                this.backName = "足";
            }
            else {
                this.frontName = "足";
                this.backName = "冰";
            }
        }

        @Override
        public void flip() {
            String tempName = frontName;
            this.frontName = backName;
            this.backName = tempName;
            this.side = side == 1 ? 2 : 1; //冰/足的翻转会切换阵营
            this.flipStep = room.step; //记录翻转的回合数
        }

        @Override
        public boolean isCanFlip() {
            return room.step - flipStep > 1; //对方上回和刚翻转的冰/足我方本回合不能立马翻回去
        }

        @Override
        public List<Integer> getMoveRange() {
            if (frontName.equals("冰") && room.cur == 1){
                return defMoveRange(new int[][]{{1, 0},{0, 1}, {0, -1}}, 2);
            }
            else if (frontName.equals("足") && room.cur == 2){
                return defMoveRange(new int[][]{{-1, 0}, {0, 1},{0, -1}}, 2);
            }
            else {
                return new ArrayList<>(); //对方选中你时只能翻转不能移动
            }
        }

        @Override
        public List<Integer> getEatRange() {
            if (frontName.equals("冰") && room.cur == 1){
                return defEatRange(new int[][]{{1, 0},{0, 1}, {0, -1}}, 2);
            }
            else if (frontName.equals("足") && room.cur == 2){
                return defEatRange(new int[][]{{-1, 0}, {0, 1},{0, -1}}, 2);
            }
            else {
                return new ArrayList<>(); //对方选中你时只能翻转不能吃
            }
        }

    }
}
